<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card>
        <el-row type="flex" justify="space-between">
          <el-col :span="20">
            <svg-icon icon-class="user" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <el-tree
          :data="list"
          :default-expand-all="true"
        >
          <template slot-scope="{node, data}">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <svg-icon icon-class="user" /><span>{{ node.data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length===0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>

    </div>
    <el-dialog
      :title=" isEdit ? '编辑' : '添加'"
      :visible.sync="showDialog"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      @close="hDialogClose"
    >
      <AddorEdit
        ref="deptDialog"
        :pid="curId"
        :is-edit="isEdit"
        :origin-list="originList"
        @update-depart="hUpdateDepart"
        @close="hCancel"
      />
    </el-dialog>
  </div>
</template>

<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { tranListToTreeData } from '@/utils/index'
import AddorEdit from './deptDialog.vue'

export default {
  name: 'Departments',
  components: {
    AddorEdit
  },

  data() {
    return {
      list: [],
      showDialog: false, // 是否显示弹层
      curId: null,
      isEdit: false,
      originList: []

    }
  },

  created() {
    this.loadDepartments()
  },
  methods: {

    // 添加新部门
    hAdd(id) {
      this.showDialog = true
      // 把id传给子组件，在子组件中去获取详情
      this.curId = id
      this.isEdit = false
    },
    // 编辑修改部门
    hEdit(id) {
      this.showDialog = true
      // 把id传给子组件，在子组件中去获取详情
      this.curId = id
      this.isEdit = true
      //  强制让子组件去发请求获取详情
      this.$nextTick(() => {
        this.$refs.deptDialog.loadDetail()
        console.log('获取子组件的引用', this.$refs.deptDialog)
      })
    },
    async loadDepartments() {
      // const obj = {}
      try {
        const res = await getDepartments()
        this.list = res.data.depts
        this.list.shift()
        //  { id, pid,code,name } => {return {id,name,code,pid}}
        this.originList = res.data.depts.map(item => {
          return {
            id: item.id,
            pid: item.pid,
            code: item.code,
            name: item.name
          }
        })
        console.log('map', this.originList)
        this.list = tranListToTreeData(res.data.depts)
        console.log('树状', this.list)
      } catch (err) {
        console.log(err)
      }
    },
    // 接收的子组件 传过来的自定义事件
    hUpdateDepart() {
      // 关闭弹层
      this.showDialog = false
      // 更新数据
      this.loadDepartments()
    },
    // 根据id删除部门
    async doDel(id) {
      try {
        const res = await delDepartment(id)
        console.log(res, 'id是', id)
        this.$message({ type: 'success', message: '删除成功!' })
        this.loadDepartments()
      } catch (err) {
        console.log(err)
        this.$message({ type: 'error', message: '删除失败!' })
      }
    },
    // 弹出是否删除提示框
    hDel(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        this.doDel(id)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 子组件点击取消按钮，关闭弹层
    hCancel() {
      this.showDialog = false
    },
    hDialogClose() {
      this.$refs.deptDialog.resetForm()
      console.log('清空----------')
    }
  }
}
</script>

<style lang="scss" scoped>
.departments {
  &-container {
    margin: 30px;
  }
  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
</style>
